package com.yt.fras.service.impl;

import com.baidu.aip.face.AipFace;
import com.yt.fras.service.FaceServer;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * ClassName:FaceServerImpl
 * Package:com.yt.fras.service.impl
 * Description:
 *
 * @date:2021/11/1 22:16
 * @author:syq
 */
@Service
public class FaceServerImpl implements FaceServer {

    /**
     * 获得远程服务器的api
     */
    private AipFace client = new AipFace("XXXXX", "XXXXX", "XXXXX");

    /**
     * @param userId 注意：这个userId是会出现在json字符串的
     * @param image  web传来的图片
     * @return
     */
    @Override
    public Boolean faceRegister(String userId, String image) {


        //options里存放着对人脸的选项
        HashMap<String, String> options = new HashMap<>();
        //精度控制
        options.put("quality_control", "NORMAL");
        //活体检测
        options.put("liveness_control", "LOW");

        //往人脸库中添加人脸
        JSONObject res = client.addUser(image, "BASE64", "face", userId, options);

        //返回的错误码
        Integer errorCode = res.getInt("error_code");

        System.out.println("res = " + res);

        //返回结果error_code=“0”表示成功
        return errorCode == 0 ? true : false;

    }

    /**
     * 人脸对比检查是否是本人
     *
     * @param image 安卓端传来的图片
     * @return
     */
    @Override
    public Boolean faceCheck(String image) {

        HashMap<String, String> options = new HashMap<>();
        options.put("quality_control", "NORMAL");
        //活体检测
        options.put("liveness_control", "LOW");

        JSONObject res = client.detect(image, "BASE64", null);

        if (res.has("error_code") && res.getInt("error_code") == 0) {

            JSONObject resultObject = res.getJSONObject("result");
            //如果找的图片是一张，则说明人脸对比成功
            Integer faceNum = resultObject.getInt("face_num");

            return faceNum == 1 ? true : false;
        } else {
            return false;
        }
    }

    @Override
    public String faceSearch(String image) {

        HashMap<String, String> options = new HashMap<>();
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "LOW");


        JSONObject res = client.search(image, "BASE64", "face", null);
        System.out.println(res);

        if (res.has("error_msg") && res.getInt("error_code") == 0) {


            JSONObject resultObject = res.getJSONObject("result");
            JSONArray userList = resultObject.getJSONArray("user_list");

            if (userList.length() > 0) {
                //第一个人脸是人脸识别相似度最高的人脸
                JSONObject user = userList.getJSONObject(0);

                double score = user.getDouble("score");
                if (score > 80) {
                    return user.getString("user_id");
                }
            }

        }
        return null;

    }

    @Override
    public Boolean faceUpdate(String userId, String image) {


        HashMap<String, String> options = new HashMap<>();
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "LOW");

        JSONObject res = client.updateUser(image, "BASE64", "face", userId, options);

        System.out.println("res = " + res);

        Integer errorCode = res.getInt("error_code");

        String errorMsg=res.getString("error_msg");

        if(StringUtils.isNotBlank(errorMsg)){
            if("user not exist".equals(errorMsg)){
                res = client.addUser(image, "BASE64", "face", userId, options);
                errorCode = res.getInt("error_code");
            }

        }

        return errorCode == 0 ? true : false;

    }

}
